<template>
  <div>
    <BasicTable :search-info="searchInfo" @register="registerTable">
      <template #toolbar>
        <a-button
          v-auth="['system:dict:data:create']"
          type="primary"
          pre-icon="ion:add"
          @click="handleCreate(null)"
        >
          {{ t('common.create') }}
        </a-button>
      </template>
      <!-- 状态 -->
      <template #isStop="{ record }">
        <Tag :color="record.isStop ? 'success' : 'error'">
          {{ record.isStop ? t('common.enabled') : t('common.disabled') }}
        </Tag>
      </template>
      <template #action="{ record }">
        <TableAction
          :actions="[
            {
              icon: 'ion:add',
              label: t('common.create'),
              onClick: handleCreate.bind(null, record),
              aUth: 'system:dict:data:create',
              show: props.searchInfo?.universal == 'folder',
            },
            {
              icon: 'ion:ios-compose-outline',
              label: t('common.edit'),
              onClick: handleEdit.bind(null, record),
              auth: 'system:dict:data:update',
            },
            {
              icon: 'ion:ios-trash-outline',
              danger: true,
              label: t('common.delete'),
              popConfirm: {
                title: t('common.confirmDelete'),
                placement: 'left',
                confirm: handleDelete.bind(null, record),
              },
              auth: 'system:dict:data:delete',
            },
          ]"
        />
      </template>
    </BasicTable>
    <DictDataModal @register="registerModal" @success="reload()" />
  </div>
</template>

<script setup lang="ts">
  import { watch } from 'vue';
  import { Tag } from 'ant-design-vue';
  import DictDataModal from './DictDataModal.vue';
  import { dataColumns, dataSearchFormSchema } from './dictData';
  import { useI18n } from '/@/hooks/web/useI18n';
  import { useMessage } from '/@/hooks/web/useMessage';
  import { useModal } from '/@/components/Modal';
  import { BasicTable, TableAction, useTable } from '/@/components/Table';
  import { GetResourceTypeById, DelResourceTaskUse } from '/@/api/demo/dict';

  defineOptions({ name: 'SystemDictData' });

  const props = defineProps({
    searchInfo: {
      type: Object as PropType<any>,
      default: () => ({}),
    },
  });

  const { t } = useI18n();
  const { createMessage } = useMessage();
  const [registerModal, { openModal }] = useModal();

  const [registerTable, { reload }] = useTable({
    // title: t('routes.table.dictionaryManagement.dictionaryDataList'),
    titleHelpMessage: 'DictDataIndex',
    api: GetResourceTypeById,
    columns: dataColumns,
    formConfig: {
      labelWidth: 120,
      schemas: dataSearchFormSchema,
      autoSubmitOnEnter: true,
    },
    rowKey: 'id',
    isTreeTable: true,
    // 分页配置
    pagination: { pageSize: 50, defaultPageSize: 50 },
    childrenColumnName: 'listson',
    useSearchForm: true,
    showTableSetting: true,
    showIndexColumn: false,
    actionColumn: {
      width: 180,
      title: t('common.action'),
      dataIndex: 'action',
      fixed: 'right',
      slots: { customRender: 'action' },
    },
  });

  function handleCreate(record) {
    console.log(record);
    openModal(true, {
      record: props.searchInfo,
      isUpdate: false,
      newRecord: record,
    });
  }

  function handleEdit(record: Recordable) {
    openModal(true, { record, isUpdate: true });
  }

  async function handleDelete(record: Recordable) {
    await DelResourceTaskUse(record.id);
    createMessage.success(t('common.delSuccessText'));
    reload();
  }

  watch(
    () => props.searchInfo,
    (val) => {
      val && reload();
    },
    { deep: true },
  );
</script>

<style scoped></style>
